package com.zzmcc.jms.controller;

import com.jfinal.aop.Before;
import com.jfinal.ext.render.excel.JxlsRender;
import com.jfinal.kit.PathKit;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.zzmcc.base.BaseController;
import com.zzmcc.common.AuthorizeInterceptor;
import com.zzmcc.jms.model.Project;
import com.zzmcc.sys.model.Userinfo;
import util.ChristStringUtil;
import util.ModelUtils;
import util.StringUtil;

import java.util.*;

import static com.zzmcc.jms.common.ModelConfigWms.TABLE_Project;


public class ProjectSelectController extends BaseController {
    private static final Logger log =  Logger.getLogger(ProjectSelectController.class);

	public void index() {

        StringBuffer whee = new StringBuffer();
        List<Object> param = new ArrayList<Object>();
        String name = getPara("demand.name");
		String demandMemo = getPara("demand.demandMemo");
		String demandType = getPara("demand.demandType");
		String isSelect = getPara("demand.isSelect");
		String isSelPass = getPara("demand.isSelPass");
		String isSelectConfirm = getPara("demand.isSelectConfirm");
		String isTask = getPara("demand.isTask");
		String isCancel = getPara("demand.isCancel");
		String isCm = getPara("demand.isCm");
		String isCt = getPara("demand.isCt");
		String isCu = getPara("demand.isCu");


		String cmCreateDate = getPara("demand.cmCreateDate");
		String ctCreateDate = getPara("demand.ctCreateDate");
		String cuCreateDate = getPara("cmCreateDate.cuCreateDate");

		String demandOwner = getPara("demand.demandOwner");
        String beginDate = getPara("beginDate");
		String endDate = getPara("endDate");
		String homeCounty = getPara("demand.homeCounty");

        if(ChristStringUtil.isNotEmpty(name)){
            whee.append(" and name like ?");
            param.add("%" + name + "%");
        }

		if(ChristStringUtil.isNotEmpty(demandMemo)){
			whee.append(" and demandMemo like ?");
			param.add("%" + demandMemo + "%");
		}

		if(ChristStringUtil.isNotEmpty(demandType)){
			whee.append(" and demandType = ?");
			param.add(demandType);
		}


		if(ChristStringUtil.isNotEmpty(demandOwner)){
			whee.append(" and demandOwner = ?");
			param.add(demandOwner);
		}

		if(ChristStringUtil.isNotEmpty(homeCounty)){
			whee.append(" and homeCounty = ?");
			param.add(homeCounty);
		}
		if(ChristStringUtil.isNotEmpty(isSelect)){
			whee.append(" and coalesce(isSelect,'否') = ?");
			param.add(isSelect);
		}
		if(ChristStringUtil.isNotEmpty(isSelectConfirm)){
			whee.append(" and coalesce(isSelectConfirm,'否') = ?");
			param.add(isSelectConfirm);
		}
		if(ChristStringUtil.isNotEmpty(isSelPass)){
			whee.append(" and coalesce(isSelPass,'否') = ?");
			param.add(isSelPass);
		}
		if(ChristStringUtil.isNotEmpty(isCancel)){
			whee.append(" and coalesce(isCancel,'否') = ?");
			param.add(isCancel);
		}
		if(ChristStringUtil.isNotEmpty(isTask)){
			whee.append(" and coalesce(isTask,'否') = ?");
			param.add(isTask);
		}

		if(ChristStringUtil.isNotEmpty(isCm)){
			whee.append(" and coalesce(isCm,'否') = ?");
			param.add(isCm);
		}
		if(ChristStringUtil.isNotEmpty(isCt)){
			whee.append(" and  coalesce(isCt,'否') = ?");
			param.add(isCt);
		}
		if(ChristStringUtil.isNotEmpty(isCu)){
			whee.append(" and  coalesce(isCu,'否') = ?");
			param.add(isCu);
		}

		if(ChristStringUtil.isNotEmpty(cmCreateDate)){
			whee.append(" and TO_DAYS(cmCreateDate) = TO_DAYS(?)");
			param.add( cmCreateDate );
		}
		if(ChristStringUtil.isNotEmpty(ctCreateDate)){
			whee.append(" and TO_DAYS(ctCreateDate) = TO_DAYS(?)");
			param.add( ctCreateDate );
		}
		if(ChristStringUtil.isNotEmpty(cuCreateDate)){
			whee.append(" and TO_DAYS(cuCreateDate) = TO_DAYS(?)");
			param.add( cuCreateDate );
		}

		if(ChristStringUtil.isNotEmpty(beginDate)){
			whee.append(" and TO_DAYS(createDate) >= TO_DAYS(?)");
			param.add( beginDate );
		}
		if(ChristStringUtil.isNotEmpty(endDate)){
			whee.append(" and TO_DAYS(createDate) <= TO_DAYS(?)");
			param.add(endDate);
		}
		Page<Project> pager = Project.dao.paginate(
				getParaToInt("pageNum", 1),
				getParaToInt("numPerPage", 20),
                "select * ",
                " from " + TABLE_Project +" where 1=1 "+ whee.toString()+"order by selectDate desc",
                param.toArray());

		setAttr("pager",pager);
		setAttr("name",name);
		setAttr("demandMemo",demandMemo);
		setAttr("demandType",demandType);
		setAttr("demandOwner",demandOwner);
		setAttr("isSelect",isSelect);
		setAttr("isSelPass",isSelPass);
		setAttr("isSelectConfirm",isSelectConfirm);
		setAttr("isTask",isTask);
		setAttr("isCancel",isCancel);
		setAttr("isCm",isCm);
		setAttr("isCt",isCt);
		setAttr("isCu",isCu);

		setAttr("beginDate",beginDate);
		setAttr("endDate",endDate);
		setAttr("homeCounty",homeCounty);
		render("../project_select_list.html");

	}

	@Before(AuthorizeInterceptor.class)
	public void edit() {
		//筛查
		Project project = Project.dao.findById(getPara("id"));
		Userinfo loginUser =getSessionAttr("loginUser");
		String modifyUser=loginUser.get("realname");
		int roleid=loginUser.get("roleid");
		//管理员及区域负责人才有权限修改
		if (roleid==0||(StringUtil.isNotEmpty(modifyUser)&&modifyUser.equals(project.getStr("projectOwner")))){
			setAttr("modifyUser",modifyUser);
			setAttr("roleid",roleid);
			setAttr("project",project);
			render("../project_select_input.html");
		} else {
			render("../project_operate_tip.html");
		}

	}

	@Before(AuthorizeInterceptor.class)
	public void sel() {
		//筛查确认
		Project m = Project.dao.findById(getPara("id"));
		String isSelect = m.getStr("isSelect");
		if (StringUtil.isNotEmpty(isSelect)&&isSelect.equals("是")){
			setAttr("project",m);
			render("../project_selconfirm_input.html");
		} else {
			render("../project_operate_tip.html");
		}

	}

	public void update() {
		Project s = getModel(Project.class, "project");
		//更新状态，同时更新完成时间
		String isSelect = s.getStr("isSelect");
		if (StringUtil.isNotEmpty(isSelect) && isSelect.equals("是")){
			s.set("selectDate", new Date());
		}
		ModelUtils.fillForUpdate(s);
		s.update();
		renderDWZSuccessJson("修改成功！");
	}

	public void updateSel() {

		Project m = getModel(Project.class, "project");
		//更新状态，同时更新完成时间
		String isSelectConfirm = m.getStr("isSelectConfirm");
		if (StringUtil.isNotEmpty(isSelectConfirm) && isSelectConfirm.equals("是")){
			Record r = Db.findFirst("select UUID_SHORT() as uuid");
			m.set("pid",r.get("uuid"));
			//m.set("isSelPass",'是');
			m.set("selectConfirmDate",new Date());
		}
		ModelUtils.fillForUpdate(m);
		m.update();
		renderDWZSuccessJson("筛查确认成功！");
	}

	@Before(AuthorizeInterceptor.class)
	public void add() {
		render("../demand_input_area.html");
	}

	public void save() {
		Project s = getModel(Project.class, "project");
		ModelUtils.fillForSave(s);
		s.save();
		renderDWZSuccessJson("添加成功！");
	}

	@Before(AuthorizeInterceptor.class)
	public void imp() {
		render("../demand_input_imp.html");
	}

	public void export() {

		StringBuffer whee = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		String name = getPara("demand.name");
		String demandMemo = getPara("demand.demandMemo");
		String demandType = getPara("demand.demandType");
		String isSelect = getPara("demand.isSelect");
		String isSelPass = getPara("demand.isSelPass");
		String isSelectConfirm = getPara("demand.isSelectConfirm");
		String isTask = getPara("demand.isTask");
		String isCancel = getPara("demand.isCancel");
		String isCm = getPara("demand.isCm");
		String isCt = getPara("demand.isCt");
		String isCu = getPara("demand.isCu");


		String cmCreateDate = getPara("demand.cmCreateDate");
		String ctCreateDate = getPara("demand.ctCreateDate");
		String cuCreateDate = getPara("cmCreateDate.cuCreateDate");

		String demandOwner = getPara("demand.demandOwner");
		String beginDate = getPara("beginDate");
		String endDate = getPara("endDate");
		String homeCounty = getPara("demand.homeCounty");

		if(ChristStringUtil.isNotEmpty(name)){
			whee.append(" and name like ?");
			param.add("%" + name + "%");
		}

		if(ChristStringUtil.isNotEmpty(demandMemo)){
			whee.append(" and demandMemo like ?");
			param.add("%" + demandMemo + "%");
		}

		if(ChristStringUtil.isNotEmpty(demandType)){
			whee.append(" and demandType = ?");
			param.add(demandType);
		}


		if(ChristStringUtil.isNotEmpty(demandOwner)){
			whee.append(" and demandOwner = ?");
			param.add(demandOwner);
		}

		if(ChristStringUtil.isNotEmpty(homeCounty)){
			whee.append(" and homeCounty = ?");
			param.add(homeCounty);
		}
		if(ChristStringUtil.isNotEmpty(isSelect)){
			whee.append(" and coalesce(isSelect,'否') = ?");
			param.add(isSelect);
		}
		if(ChristStringUtil.isNotEmpty(isSelectConfirm)){
			whee.append(" and coalesce(isSelectConfirm,'否') = ?");
			param.add(isSelectConfirm);
		}
		if(ChristStringUtil.isNotEmpty(isSelPass)){
			whee.append(" and coalesce(isSelPass,'否') = ?");
			param.add(isSelPass);
		}
		if(ChristStringUtil.isNotEmpty(isCancel)){
			whee.append(" and coalesce(isCancel,'否') = ?");
			param.add(isCancel);
		}
		if(ChristStringUtil.isNotEmpty(isTask)){
			whee.append(" and coalesce(isTask,'否') = ?");
			param.add(isTask);
		}

		if(ChristStringUtil.isNotEmpty(isCm)){
			whee.append(" and coalesce(isCm,'否') = ?");
			param.add(isCm);
		}
		if(ChristStringUtil.isNotEmpty(isCt)){
			whee.append(" and  coalesce(isCt,'否') = ?");
			param.add(isCt);
		}
		if(ChristStringUtil.isNotEmpty(isCu)){
			whee.append(" and  coalesce(isCu,'否') = ?");
			param.add(isCu);
		}

		if(ChristStringUtil.isNotEmpty(cmCreateDate)){
			whee.append(" and TO_DAYS(cmCreateDate) = TO_DAYS(?)");
			param.add( cmCreateDate );
		}
		if(ChristStringUtil.isNotEmpty(ctCreateDate)){
			whee.append(" and TO_DAYS(ctCreateDate) = TO_DAYS(?)");
			param.add( ctCreateDate );
		}
		if(ChristStringUtil.isNotEmpty(cuCreateDate)){
			whee.append(" and TO_DAYS(cuCreateDate) = TO_DAYS(?)");
			param.add( cuCreateDate );
		}

		if(ChristStringUtil.isNotEmpty(beginDate)){
			whee.append(" and TO_DAYS(createDate) >= TO_DAYS(?)");
			param.add( beginDate );
		}
		if(ChristStringUtil.isNotEmpty(endDate)){
			whee.append(" and TO_DAYS(createDate) <= TO_DAYS(?)");
			param.add(endDate);
		}

		String sql = "select *  from " + TABLE_Project +" where 1=1 "+ whee.toString()+"order by modifyDate desc";
		List<Record> demandDetail = Db.find(sql, param.toArray());

		Map<String, Object> beans = new HashMap<String, Object>();
		beans.put("demandDetail", demandDetail);

		String templateFileName = PathKit.getRootClassPath() + "/tpl_project_demand.xls";
		String filename = "project_demand.xls";
		render(JxlsRender.me(templateFileName).filename(filename).beans(beans));


	}

	@Before(AuthorizeInterceptor.class)
	public void transConfirm() {
		//筛查确认
		String[] ids = getParaValues("ids");

		for(String id:ids) {
			Project m = Project.dao.findById(id);
			String isSelect = m.getStr("isSelect");
			String isSelectConfirm = m.getStr("isSelectConfirm");
			if (StringUtil.isEmpty(isSelectConfirm)) {
				isSelectConfirm = "否";
			}
			if ( isSelectConfirm.equals("否")&& isSelect.equals("是")){
				Record r = Db.findFirst("select UUID_SHORT() as uuid");
				m.set("pid",r.get("uuid"));
				m.set("isSelectConfirm", "是");
				m.set("selectConfirmDate",new Date());
			}
			ModelUtils.fillForUpdate(m);
			m.update();
		}
		renderDWZSuccessJson("批量筛查确认成功!");

	}


}
